package demo.controller;

import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController  {
    @Autowired
    private DataSourceTransactionManager manager;
    @Autowired
    private TransactionDefinition definition;

    @Autowired
    private UserService userService;

    @RequestMapping("register")
    public Boolean register(String userName, String password){
        //开启事务
        TransactionStatus transaction = manager.getTransaction(definition);
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        //回滚事务
        manager.rollback(transaction);
        //提交事务
        manager.commit(transaction);
        return true;
    }

}
